package com.shujia.MR.hw;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class JoinReducer extends Reducer<Text, Text, Text, Text> {
    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        String studentInfo = null;
        List<String> studentScores = new ArrayList<>();

        for (Text value : values) {
            String[] value_splits = value.toString().split("\t");
            if (value_splits.length > 2) {
                studentInfo = value.toString();
            } else {
                studentScores.add(value.toString());
            }
        }
        for (String studentScore : studentScores) {
            context.write(key, new Text(studentInfo + "\t" + studentScore));
        }
    }
}
